package com.otitan.la.forest.industry.client.http;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import com.otitan.la.forest.industry.client.http.methods.AbstractHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: HttpClient
 * @Author: jiachangsen
 * @CreateDate: 2020/10/15 11:31
 * @UpdateUser: jiacs
 * @UpdateDate: 2020/10/15 11:31
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class HttpGet extends AbstractHttpClient {
    private String path;

    private String token;

    private Map<String, Object> params;

    public HttpGet(String path, String token, Map<String, Object> params) {
        super(path, token, params);
        this.path = path;
        this.token = token;
        this.params = params;
    }

    private org.apache.http.client.methods.HttpGet get;

    @Override
    public void execute() {
        URIBuilder uriBuilder = null;
        ByteArrayOutputStream bos = null;
        try {
            uriBuilder = new URIBuilder(path);
            if (params != null) {
                List<NameValuePair> nameValuePairs = new ArrayList<>();
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
                }
                uriBuilder.setParameters(nameValuePairs);
            }
            get = new org.apache.http.client.methods.HttpGet(uriBuilder.build());

            if (StrUtil.isNotEmpty(token)) {
                get.setHeader(Header.AUTHORIZATION.getValue(), token);
            }

            //设置超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
                    .setSocketTimeout(CONNECT_TIMEOUT).build();
            get.setConfig(requestConfig);

            HttpClient client = HttpClientBuilder.create().build();
            HttpResponse response = client.execute(get);
            statusCode = response.getStatusLine().getStatusCode();
            httpEntity = response.getEntity();

            if (statusCode >= 400) {
                log.error("【请求错误】 状态码：{}, ", dataStr);
            }

        } catch (ClientProtocolException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        } catch (URISyntaxException e) {
            log.error(e.getMessage());
        } finally {

            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public void releaseConnection() {
        if (get != null){
            get.releaseConnection();
        }
    }
}
